/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.univaq.f4i.iw.ex.album.model.impl;

import it.univaq.f4i.iw.ex.album.model.Guest;
import it.univaq.f4i.iw.ex.album.model.Invitation;
import it.univaq.f4i.iw.ex.album.model.MeetingRoom;
import it.univaq.f4i.iw.ex.album.model.Organizer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 *
 * @author juli
 */
public class InvitationMysqlImpl implements Invitation{
    private int key;
    private Organizer organizer;
    private String key_org;
    private List<Guest> guests;
    private MeetingRoom meetingRoom;
    private int key_mr;
    private Date date;
    private Time startTime;
    private Time endTime;
    private MeetingDataLayerMysqlImpl datalayer;

    InvitationMysqlImpl(MeetingDataLayerMysqlImpl datalayer) {
        key = 0;
        organizer = null;
        key_org = "";
        guests = null;
        meetingRoom = null;
        key_mr = 0;
        date = null;
        this.datalayer = datalayer;
    }

    InvitationMysqlImpl(MeetingDataLayerMysqlImpl datalayer, ResultSet data) throws SQLException {
        this.datalayer = datalayer;
        key = data.getInt("ID");
        organizer = null;
        key_org = data.getString("key_org");
        guests = null;
        meetingRoom = null;
        key_mr = data.getInt("key_mr");
        SimpleDateFormat textFormat = new SimpleDateFormat("dd-MM-yyyy");
        this.date = null;
        try{
            this.date = textFormat.parse(data.getString("date"));
        }catch(ParseException ex){
            System.out.println("Error at trying to get Date/Time: " + ex.getMessage());
        }
        
        try {
            SimpleDateFormat sdf = new java.text.SimpleDateFormat("hh:mm:ss", new Locale("es", "ES"));
            this.startTime = new java.sql.Time(sdf.parse(data.getString("startTime")).getTime());
        } catch (Exception ex) {
            System.out.println("Error at trying to get Date/Time: " + ex.getMessage());
        }
        
        try {
            SimpleDateFormat sdf = new java.text.SimpleDateFormat("hh:mm:ss", new Locale("es", "ES"));
            this.endTime = new java.sql.Time(sdf.parse(data.getString("endTime")).getTime());
        } catch (Exception ex) {
            System.out.println("Error at trying to get Date/Time: " + ex.getMessage());
        }
    }

    public int getKey() {
        return this.key;
    }

    public void setKey(int key) {
        this.key=key;
    }
   
    public Organizer getOrganizer() {
        if (organizer == null) {
            organizer = datalayer.getOrganizer(key_org);
        }
        return organizer;
    }

    public List<Guest> getGuests() {
        if (guests == null) {
            guests = datalayer.getGuestByInvitation(this);
        }
        return guests;  
    }

    public MeetingRoom getMeetingRoom() {
        if (meetingRoom == null) {
            //meetingRoom = datalayer.getMeetingRoom(key_mr);
        }
        return meetingRoom;  
    }

    public Date getDate() {
        return this.date;
    }

    public void setOrganizer(Organizer o) {
        this.organizer = o;
    }

    public void setMeetingRoom(MeetingRoom m) {
        this.meetingRoom = m;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Time getStartTime() {
        return this.startTime;
    }

    public Time getEndTime() {
        return this.endTime;
    }

    public void setStartTime(Time startTime) {
        this.startTime = startTime;
    }

    public void setEndTime(Time endTime) {
        this.endTime = endTime;
    }

    public void setDate(String date) {
        SimpleDateFormat textFormat = new SimpleDateFormat("dd-MM-yyyy");
        this.date = null;
        try{
            this.date = textFormat.parse(date);
        }catch(ParseException ex){
             System.out.println("Error at trying to get Date/Time: " + ex.getMessage());
        }
    }

    public void setStartTime(String startTime) {
        try {
            SimpleDateFormat sdf = new java.text.SimpleDateFormat("hh:mm:ss", new Locale("es", "ES"));
            this.startTime = new java.sql.Time(sdf.parse(startTime).getTime());
        } catch (Exception ex) {
            System.out.println("Error at trying to get Date/Time: " + ex.getMessage());
        }
    }

    public void setEndTime(String endTime) {
        try {
            SimpleDateFormat sdf = new java.text.SimpleDateFormat("hh:mm:ss", new Locale("es", "ES"));
            this.endTime = new java.sql.Time(sdf.parse(endTime).getTime());
        } catch (Exception ex) {
            System.out.println("Error at trying to get Date/Time: " + ex.getMessage());
        }
    }

   

   

   
}
